// 引入外部资料库
import {
  getSetting,
  chooseAddress,
  openSetting,
  showModal,
  showToast,
} from "../../utils/asyncWx";
import regeneratorRuntime from "../../libs/runtime";

Page({
  data: {
    address: {},
    cart: [],
    totalPrice: 0,
    totalNum: 0,
  },

  onShow: function (options) {
    const address = wx.getStorageSync("address");

    // 获取缓存中的购物车数据
    let cart = wx.getStorageSync("cart") || [];

    // 获取要真正要购买的商品
    cart = cart.filter((v) => v.isChecked);

    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach((v) => {
      totalPrice += v.num * v.goods_price;
      totalNum += v.num;
    });
    this.setData({
      cart,
      totalPrice,
      totalNum,
      address,
    });
  },

  // 支付按钮功能实现
  handleOrderPay() {
    const token = wx.getStorageSync("token");
    if (!token) {
      wx.navigateTo({
        url: "/pages/auth/auth",
      });
      return;
    }
    console.log("token");
  },
});
